<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\book\model;

use manniu\model\Base;

use think\Cache;

class BookAutoSubscription extends Base
{
    // 自动订阅缓存前缀
    protected $basCachePrefix = 'bas_uid_';

    // 模型事件
    protected static function init()
    {
        // 钩子 - 写入后
        BookAutoSubscription::afterWrite(function ($bas) {
            $bas->setBooksCache($bas->getData('uid'));
        });

        // 钩子 - 删除前
        BookAutoSubscription::beforeDelete(function ($bas) {
            $bas->rmBooksCache($bas->getData('uid'));
        });
    }

    /**
     * 检查图书是否开启自动订阅功能
     * @param int $uid 用户id
     * @param int $book_id 图书id
     * @return bool
     */
    public function checkAutoSubscription($uid = 0, $book_id = 0)
    {
        $uid = (int)$uid;
        $book_id = (int)$book_id;
        if (!$uid || !$book_id) {
            return false;
        }
        $books = $this->getBooks($uid, false);
        if (in_array($book_id, $books)) {
            return true;
        }
        return false;
    }

    public static function closeAutoSubscription($uid, $book_id){
        $subscription = self::where(['uid'=>$uid,'book_id'=>$book_id])->find();
        if($subscription){
            $subscription->delete();
            return true;
        }
        return false;
    }

    /**
     * 获取自动订阅
     * @param int $uid
     * @param int $cache
     * @return array|void
     */
    public function getBooks($uid = 0, $cache = false)
    {
        $uid = (int)$uid;
        if (!$uid) {
            return [];
        }
        $books = $this->getBooksCache($uid);
        if (!$books) {
            $books = $this->setBooksCache($uid, $cache, true);
        }
        $books = $books ? $books : [];
        return $books;
    }

    /**
     * 获取自动订阅缓存
     * @param int $uid
     */
    protected function getBooksCache($uid = 0)
    {
        return Cache::get($this->basCachePrefix . $uid);
    }

    /**
     * 删除自动订阅缓存
     * @param int $uid 用户id
     */
    protected function rmBooksCache($uid = 0)
    {
        Cache::rm($this->basCachePrefix . $uid);
    }

    /**
     * 生成自动订阅缓存
     * @param int $uid 用户id
     */
    protected function setBooksCache($uid = 0, $cache = false, $is_return = false, $field = ['book_id'])
    {
        $books = [];
        $data = $this->field($field)->where(['uid' => $uid])->cache($cache)->select();
        if ($data) {
            foreach ($data as $item) {
                $books[] = $item->getData('book_id');
            }
            Cache::set($this->basCachePrefix . $uid, $books);
        }
        if ($is_return === true) {
            $books = $books ? $books : [];
            return $books;
        }
    }
}